package cz.najman.emdroid.old;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import cz.najman.emdroid.content.ContentHandler;
import cz.najman.emdroid.mappings.Event;

import android.content.ContentValues;
import android.util.Log;

public class EventContentHandler extends ContentHandler {
	private static final String TAG = "EventContentHandler";
	
	@Override
	public void startDocument() throws SAXException {
		
	}
	
	@Override
	public void endDocument() throws SAXException {
		
	}
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		
		final int key = getCorrespondingKey(qName);
		
		switch (key) {
		case KEY_EVENT:
			mValues = new ContentValues();
			break;
		case KEY_EVENT_ID:			
			
		case KEY_EVENT_NAME:
			
		case KEY_VISIBLE:
			
		case KEY_START_DATE:
			
		case KEY_END_DATE:
			
		case KEY_REGISTRATION_START:
			
		case KEY_REGISTRATION_END:
			mBulider.delete(0, mBulider.length());			
			break;		
		default:
			break;
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		final int key = getCorrespondingKey(qName);
		
		switch (key) {
		case KEY_EVENT:
			Log.d(TAG, "Returning");
			//mListener.onParsedDataReceive(mValues, mRequestId);
			break;
		case KEY_EVENT_ID:
			mValues.put(Event._ID, mBulider.toString());
			break;
		case KEY_EVENT_NAME:
			mValues.put(Event.EVENT_NAME, mBulider.toString());
			break;
		case KEY_VISIBLE:
			mValues.put(Event.VISIBLE, mBulider.toString());
			break;
		case KEY_START_DATE:
			mValues.put(Event.START_DATE, getTimeFromString(mBulider.toString()));
			break;
		case KEY_END_DATE:
			mValues.put(Event.END_DATE, getTimeFromString(mBulider.toString()));
			break;
		case KEY_REGISTRATION_START:
			mValues.put(Event.REGISTRATION_START, getTimeFromString(mBulider.toString()));
			break;
		case KEY_REGISTRATION_END:
			mValues.put(Event.REGISTRATION_END, getTimeFromString(mBulider.toString()));
			break;		
		default:
			break;
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		mBulider.append(ch,start,length);
	}
	
	private static final String EVENT = "event";
	private static final String EVENT_ID = "id";
	private static final String EVENT_NAME = "eventName";
	private static final String VISIBLE = "visible";
	private static final String START_DATE = "startDate";
	private static final String END_DATE = "endDate";
	private static final String REGISTRATION_START = "registrationStart";
	private static final String REGISTRATION_END = "registrationEnd";
		
	private static final int KEY_EVENT = 1;
	private static final int KEY_EVENT_ID = 2;
	private static final int KEY_EVENT_NAME = 3;
	private static final int KEY_VISIBLE = 4;
	private static final int KEY_START_DATE = 5;
	private static final int KEY_END_DATE = 6;
	private static final int KEY_REGISTRATION_START = 7;
	private static final int KEY_REGISTRATION_END = 8;
	
	/**
	 * Method matches String <b>name</b> with corresponding int <b>key</b>
	 * @param name Name of the element
	 * @return Key corresponding int <b>key</b> to given String <b>name</b>, if corresponding key
	 * not found, returns -1
	 */
	public int getCorrespondingKey(String name){
		
		if(name.equals(EVENT)){
			return KEY_EVENT;
		} else if(name.equals(EVENT_ID)){
			return KEY_EVENT_ID;
		} else if(name.equals(EVENT_NAME)){
			return KEY_EVENT_NAME;
		} else if(name.equals(VISIBLE)){
			return KEY_VISIBLE;
		} else if(name.equals(START_DATE)){
			return KEY_START_DATE;
		} else if(name.equals(END_DATE)){
			return KEY_END_DATE;
		} else if(name.equals(REGISTRATION_START)){
			return KEY_REGISTRATION_START;
		} else if(name.equals(REGISTRATION_END)){
			return KEY_REGISTRATION_END;
		} 
		
		return -1;
	}
	
}
